<?php


namespace app\api\controller;

use app\common\controller\Api;
use think\checkdate;

/**
 * 文章接口
 */
class Article extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * Notice模型对象
     * @var \app\admin\model\Notice
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Notice;
    }


    /**
     * 公告列表
     * @param string $limit  页数列表显示条数
     * @param string $paging 页数
     */
    public function notice_index()
    {
        $limit = intval($this->request->request('limit'));
        $paging = intval($this->request->request('paging'));
        $limit = $limit ? $limit : 10;
        $paging = $paging ? 10 * $paging : 0;
        // ->field('id', 'title', 'updatetime', 'text')
        $data = $this->model
        ->where('status','normal')
        ->where('sort_id','14')
        ->limit($paging,$limit)
        ->order('updatetime', 'desc')
        ->order('id', 'desc')
        // ->cache(true)
        ->select();
        if($data){
            foreach($data as &$v){
                unset($v['createtime']);
                unset($v['status']);
                unset($v['sort_id']);
                unset($v['content']);
                $v['updatetime'] = datetime($v['updatetime']);
                if($v['img']){
                    $v['img'] = request()->domain().$v['img'];
                }
            }
        }
        
        $this->success('',$data);
    }

    /**
     * 公告详情
     * @param string $id id
     */
    public function notice_details()
    {
        $id = intval($this->request->request('id'));
        if(!$id){
            $this->error('参数不正确');
        }
        $data = $this->model
        ->where('status','normal')
        ->where('sort_id','14')
        // ->cache(true)
        ->find($id);

        if(!$data){
            $this->error('内容不存在');
        }

        unset($data['createtime']);
        unset($data['status']);
        unset($data['sort_id']);
        // unset($data['content']);
        // unset($data['title']);

        unset($data['img']);
        unset($data['synopsis']);
        $data['content'] = preg_replace('/(<img.+?src=")((?!(https|http))[^>]+)/','$1'.request()->domain().'$2', $data['content']);
        $data['updatetime'] = datetime($data['updatetime']);
        
           
        $this->success('',$data);
    }


    /**
     * 帮助中心列表
     * @param string $limit  页数列表显示条数
     * @param string $paging 页数
     */
    public function guide_index()
    {
        $limit = intval($this->request->request('limit'));
        $paging = intval($this->request->request('paging'));
        $limit = $limit ? $limit : 10;
        $paging = $paging ? 10 * $paging : 0;
        // ->field('id', 'title', 'updatetime', 'text')
        $data = $this->model
        ->where('status','normal')
        ->where('sort_id','15')
        ->limit($paging, $limit)
        ->order('updatetime', 'desc')
        ->order('id', 'desc')
        ->cache(true)
        ->select();

        if($data){
            foreach($data as &$v){
                unset($v['createtime']);
                unset($v['status']);
                unset($v['sort_id']);
                unset($v['content']);
    
                $v['updatetime'] = datetime($v['updatetime']);
                if($v['img']){
                    $v['img'] = request()->domain().$v['img'];
                }
            }
        }
        
        $this->success('',$data);
    }

    /**
     * 帮助中心详情
     * @param string $id id
     */
    public function guide_details()
    {
        $id = intval($this->request->request('id'));
        if(!$id){
            $this->error('参数不正确');
        }
        $data = $this->model
        ->where('status','normal')
        ->where('sort_id','15')
        ->cache(true)
        ->find($id);

        if(!$data){
            $this->error('内容不存在');
        }

        unset($data['createtime']);
        unset($data['status']);
        unset($data['sort_id']);
        // unset($data['content']);
        // unset($data['title']);

        unset($data['img']);
        unset($data['synopsis']);
        $data['content'] = preg_replace('/(<img.+?src=")((?!(https|http))[^>]+)/','$1'.request()->domain().'$2', $data['content']);
        $data['updatetime'] = datetime($data['updatetime']);
           
        $this->success('',$data);
    }


    /**
     * 广告位详情
     * @param string $id id
     */
    public function details()
    {
        $id = intval($this->request->request('id'));
        if(!$id){
            $this->error('参数不正确');
        }
        $data = $this->model
        ->where('status','normal')
        // ->cache(true)
        ->find($id);

        if(!$data){
            $this->error('内容不存在');
        }

        unset($data['createtime']);
        unset($data['status']);
        unset($data['sort_id']);
        // unset($data['content']);
        // unset($data['title']);

        unset($data['img']);
        unset($data['synopsis']);
        $data['content'] = preg_replace('/(<img.+?src=")((?!(https|http))[^>]+)/','$1'.request()->domain().'$2', $data['content']);
        $data['updatetime'] = datetime($data['updatetime']);
        
           
        $this->success('',$data);
    }

    

}
